'use strict';

const Controller = require('egg').Controller;
const moment = require('moment');

class ScoreController extends Controller {
    //增删改查
    //增加  post
    async create() {
        let {ctx,service} = this;

        let {username,skill,theory,num} = ctx.request.body;
        let {rid} = ctx.info;
        if(rid === 1 || rid === 2){
            //有权利去添加 录入成绩
            if(username && (skill >= 0 && skill <= 100) && (theory >= 0 && theory <= 100) && num){
                try{
                    await service.score.add(username,skill,theory,num);
                    ctx.body = {
                        code:1,
                        msg:'添加成功'
                    }    
                }catch(e){
                    ctx.body = {
                        code:0,
                        msg:'添加失败'
                    }
                }
                
            }else{
                ctx.body = {
                    code:1,
                    msg:'参数有误'
                }
            }
        }else{
            ctx.body = {
                code:6,
                msg:'您没有权限去添加'
            }
        }
       
    }
    //删除  delete
    async destroy(){
        let {ctx,service} = this;
        let {id} = ctx.params;
        if(id){
            try{
                await service.score.del(id);
                ctx.body = {
                    code:1,
                    msg:'删除成功'
                }
            }catch(e){
                ctx.body = {
                    code:0,
                    msg:e
                }
            }
        }else{
            ctx.body = {
                code:1,
                msg:'参数有误'
            }
        }
    }
    //更新  put
    async update(){
        let {ctx,service} = this;
        let {username,skill,theory} = ctx.request.body;
        let {id} = ctx.params;
        console.log(id);
        if(username && (skill >= 0 && skill <= 100) && (theory >= 0 && theory <= 100) && id){
            try{
                await service.score.test(username,skill,theory,id);
                ctx.body = {
                    code:1,
                    msg:'修改成功'
                }    
            }catch(e){
                ctx.body = {
                    code:0,
                    msg:'修改失败'
                }
            }
            
        }else{
            ctx.body = {
                code:1,
                msg:'参数有误'
            }
        }
    }
    //查询和模糊搜索  get
    async index(){
        let {ctx,service} = this;
        //分页  pagenum limit total
        let {pagenum=1,limit=5,key} = ctx.query;
        
        let list = await service.score.index(pagenum,limit,key);
        let total = await service.score.total(key);
        ctx.body = {
            code:1,
            data:list,
            total:total[0]['count(*)']
        }
    }
    //获取本月成绩曲线
    async line(){
        //时间 技能 理论 均为 数组  查询指定人员 and 当月所有的成绩
        let {ctx,service} = this;
        let {num} = ctx.info;
        let list = await service.score.line(num);
        let skillArr = [],theoryArr=[],time=[];
        list.forEach(item => {
            skillArr.push(item.skill);
            theoryArr.push(item.theory);
            time.push(moment(item.create_time).format('MM-DD')) //03-11
        })
        ctx.body = {
            code:1,
            data:{
                skillArr,
                theoryArr,
                time
            }
        }
    }
    //获取今日成绩分布图
    async bar(){
        let {ctx,service} = this;
        let arr = ['0-70','71-80','81-90','91-100'];
        let skillNum = [];  //技能各个阶段的数量  [10, 20, 30, 20]
        let theoryNum = []; //理论各个阶段的数量  [10, 20, 30, 20]

        for(let i=0;i<arr.length;i++){
            let num = arr[i].split('-');
            let sNum = await service.score.bar(num[0],num[1],'skill');
            let tNum = await service.score.bar(num[0],num[1],'theory');
            skillNum.push(sNum[0]['count(*)']);
            theoryNum.push(tNum[0]['count(*)']);
        }

        ctx.body = {
            code:1,
            data:{
                skillNum,
                theoryNum
            }
        }
    }
}

module.exports = ScoreController;
